#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   old_new_api_test.py    
@Contact :   fengfeng.qiu@amh-group.com

@Modify Time      @Author    
------------      -------    
2022/4/19 10:18   qiufengfeng      
'''
import pandas as pd
import json
import requests
import ast


def load_test_data(path):
    def processo_data(item):
        json_obj = ast.literal_eval(item['asr_detail'])
        result = []
        for i in json_obj:
            result.append(i['role'] + ":" + i["text"])
        return result
    df = pd.read_csv(path,'')
    df.dropna(subset=["asr_detail"],inplace=True)
    df["dialogue"] = df.apply(processo_data,axis=1)
    return df["dialogue"].tolist()


def http_post(url,dialogue_list):

    post_body = {
   "dialogue": [dialogue_list],
 }
    headers = {
        'Content-Type':"application/json"
    }

    req = requests.post(url,data=json.dumps(post_body),headers=headers)
    return req.json()

def api_result(dialogue):
    beetle_url = "http://127.0.0.1:8080/beetle/contraband/contraband_identify"
    orochi_url = 'http://localhost:8000/phantom/tf-predict'

    orochi_result = http_post(orochi_url,dialogue)
    beetle_result = http_post(beetle_url,dialogue)

    return beetle_result,orochi_result


def check_list_same(list_one,list_two):
    if len(list_one) != len(list_two):
        return False

    for item in list_one:
        if item not in list_two:
            return False

    return True



def __main__():
    data_path = './data/风控数据.csv'
    dialogues = load_test_data(data_path)
    for dialogue in dialogues:
        beetle_json,orochi_json = api_result(dialogue)
        if check_list_same(beetle_json["contraband_infos"][0]["sure_contraband"] , orochi_json["contraband_infos"][0]["sure_contraband"]) :
            print("ok")
        else:
            print(json.dumps(dialogue,ensure_ascii=False))
            raise ValueError("两次结果不一样")

if __name__ == '__main__':
    __main__()
